<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品ID" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="商品码" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入商品码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    

    <el-table v-loading="loading" :data="saleGoodsList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
     <!-- <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="商品ID" align="center" prop="goodsId" />
      <el-table-column label="商品码" align="center" prop="goodsCode" /> -->
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="原价" align="center" prop="price" />
      <el-table-column label="折扣" align="center" prop="discount" />
      <el-table-column label="现价" align="center" prop="currentPrice" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['crm:saleGoods:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['crm:saleGoods:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售产品对话框 -->
    
  </div>
</template>

<script setup name="SaleGoods">
	import {defineProps} from "vue"
import { listSaleGoods, getSaleGoods, delSaleGoods, addSaleGoods, updateSaleGoods } from "@/api/crm/saleGoods";

const { proxy } = getCurrentInstance();

const saleGoodsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    goodsId: null,
    goodsCode: null,
    goodsName: null,
    quantity: null,
    price: null,
    discount: null,
    currentPrice: null,
  },
  rules: {
  }
});


const { queryParams, form, rules } = toRefs(data);


const props = defineProps({
    orderId: {
        type: String
    }
})

/** 查询销售产品列表 */
function getList() {
  loading.value = true;
  
  console.log(props.orderId)
  console.log(queryParams)
  queryParams.value.orderId = props.orderId;
  
  listSaleGoods(queryParams.value).then(response => {
    saleGoodsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderId: null,
    goodsId: null,
    goodsCode: null,
    goodsName: null,
    quantity: null,
    price: null,
    discount: null,
    currentPrice: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("saleGoodsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加销售产品";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSaleGoods(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改销售产品";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["saleGoodsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSaleGoods(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSaleGoods(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除销售产品编号为"' + _ids + '"的数据项？').then(function() {
    return delSaleGoods(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('crm/saleGoods/export', {
    ...queryParams.value
  }, `saleGoods_${new Date().getTime()}.xlsx`)
}

getList();
</script>
